public class HelloWorld {

	public static final int NANOS_PER_MILLI = 1000000;
	
	private static long milliseconds = 0;
	private static int nanoseconds = 0;
	
	/**
	 * @param args
	 */	
	public static void main(String[] args) {
		milliseconds = 9093225;
		nanoseconds = 998784;
		
		long millis = 2000;
		int nanos = 2000;
		
		long millis_tmp;
		int nanos_tmp, millis_up;
		millis_tmp = milliseconds;
		nanos_tmp = nanoseconds;
		
		millis_tmp += millis;
		nanos_tmp += nanos;
		
		millis_up = nanos_tmp / 1000000;
		millis_tmp += millis_up;

		_setnormalised(milliseconds + millis, nanoseconds + nanos);
		//_setnormalised(millis_tmp, nanos_tmp);
		System.out.println("" + milliseconds + "ms, " + nanoseconds + "ns");
	}
	
	protected static boolean _setnormalised(long millis, int nanos)
	{
		final long millis_in_nanos = nanos / NANOS_PER_MILLI;
        final int nanosleft = (int) (nanos % NANOS_PER_MILLI);
        if (millis > 0) {
            if (nanos < 0) { // no overflow possible
                milliseconds = millis + millis_in_nanos;
                // ensure same sign
                if (milliseconds > 0 && nanosleft != 0) {
                    milliseconds--;
                    nanoseconds = nanosleft + NANOS_PER_MILLI;
                }
                else {
                    nanoseconds = nanosleft;
                }
            }
            else { // watch for overflow
                long tmp = millis + millis_in_nanos;
                if (tmp <= 0) {
//                     System.out.println("setNormalized failing: " + millis + ", " + nanos);
                    return false;
                }
                milliseconds = tmp;
                nanoseconds = nanosleft;
            }
        }
        else if (millis < 0) {
            if (nanos < 0) { // watch for negative overflow
                long tmp = millis + millis_in_nanos;
                if (tmp >= 0) {
//                     System.out.println("setNormalized failing: " + millis + ", " + nanos);
                    return false;
                }
                milliseconds = tmp;
                nanoseconds = nanosleft;
            }
            else { // no overflow possible
                milliseconds = millis + millis_in_nanos;
                // ensure same sign
                if (milliseconds < 0 && nanosleft != 0) {
                    milliseconds++;
                    nanoseconds = nanosleft - NANOS_PER_MILLI;
                }
                else {
                    nanoseconds = nanosleft;
                }
            }
        }
        else { // millis == 0
            milliseconds = millis_in_nanos;
            nanoseconds = nanosleft;
        }
        return true;
	}
}
